package com.fpc.template;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.text.csv.CsvReader;
import cn.hutool.core.text.csv.CsvUtil;
import com.fpc.template.common.FileActionListener;
import com.fpc.template.pojo.ActivityProperties;
import com.fpc.template.pojo.Images;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.nio.channels.ClosedSelectorException;
import java.util.List;

/**
 * @description: ________
 * ___  __/______________
 * __  /_ ___  __ \  ___/
 * _  __/ __  /_/ / /__
 * /_/    _  .___/\___/
 * /_/
 * @author: www.fpcs.top
 * @create: 2021-08-21 10:28
 */
public class start {
    /**
     * 简单的代码生成器.
     *
     * @param rootPath       maven 的  java 目录
     * @param templateName   模板的名称
     * @param object 需要渲染对象的封装
     * @throws IOException       the io exception
     * @throws TemplateException the template exception
     */
    public static void autoCodingJavaEntity(String rootPath,
                                            String templateName,
                                            String templatePath,
                                            Object object) throws IOException, TemplateException {

        // freemarker 配置
        Configuration configuration = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);

        configuration.setDefaultEncoding("UTF-8");
        // 指定模板的路径
        configuration.setDirectoryForTemplateLoading(new File(templatePath));
        // 根据模板名称获取路径下的模板
        Template template = configuration.getTemplate(templateName);

        // 定义一个输出流来导出代码文件
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(rootPath),"UTF-8");
        // freemarker 引擎将动态数据绑定的模板并导出为文件
        template.process(object, outputStreamWriter);

    }

    //生产页面
    public static void produceActivity(String rootPath, String templatePath,ActivityProperties cssProperties) throws IOException, TemplateException {
        String templateName = "Activity.ftl";
        autoCodingJavaEntity(rootPath+".html",templateName, templatePath,cssProperties);

    }

    //生产sj页面
    public static void produceSjActivity(String rootPath, String templatePath,ActivityProperties cssProperties) throws IOException, TemplateException {
        // 路径根据自己项目的特点调整
        String templateName = "ActivitySj.ftl";
        autoCodingJavaEntity(rootPath+"_sj.html", templateName, templatePath, cssProperties);


    }

    //生产css
    public static void produceCss(String rootPath, String templatePath,ActivityProperties cssProperties) throws IOException, TemplateException {
        // 路径根据自己项目的特点调整
        String templateName = "Css.ftl";
        autoCodingJavaEntity(rootPath+".css", templateName, templatePath, cssProperties);
    }

    public static void production(ActivityProperties activityProperties,String path1,String path2,String path3) throws IOException, TemplateException {
        // 路径根据自己项目的特点调整
        String rootPath = path1+activityProperties.getActivityUrl();

        CsvReader reader = CsvUtil.getReader();
        List<Images> result = reader.read(
                ResourceUtil.getUtf8Reader(path1), Images.class);

        activityProperties.setImages(result);
        activityProperties.setImageSize(result.size());

        //css
        produceCss(rootPath,path3,activityProperties);

        //pc
        produceActivity(rootPath,path3,activityProperties);
        //手机

        ActivityProperties activityProperties2 = new ActivityProperties();
        BeanUtil.copyProperties(activityProperties,activityProperties2);
        CsvReader reader2 = CsvUtil.getReader();
        List<Images> result2 = reader2.read(
                ResourceUtil.getUtf8Reader(path2), Images.class);
        activityProperties2.setImages(result2);
        activityProperties2.setImageSize(result2.size());
        //手机
        produceSjActivity(rootPath,path3,activityProperties2);

        //pc
        // ActivityProperties activityProperties = new ActivityProperties();


    }

    public static void main(String[] args) {
        // 主窗体
        JFrame frmMain = new JFrame("fpc模板器");
        // 主窗体设置大小
        frmMain.setSize(350, 600);
        // 主窗体设置位置
        frmMain.setLocation(200, 200);
        // 主窗体中的组件设置为绝对定位
        frmMain.setLayout(new FlowLayout(FlowLayout.CENTER));

        frmMain.add(new JLabel("fpc 模板器"));
        JPanel p1 = new JPanel();
        JLabel lblPassword1 = new JLabel("缩略名(nwsmc)");
        JTextField baobao1 = new JTextField(16);
        p1.add(lblPassword1);
        p1.add(baobao1);
        frmMain.add(p1);

        JPanel p2 = new JPanel();
        JLabel lblPassword2 = new JLabel("主题");
        JTextField baobao2 = new JTextField(16);
        p2.add(lblPassword2);
        p2.add(baobao2);
        frmMain.add(p2);

        JPanel p3 = new JPanel();
        JLabel lblPassword3 = new JLabel("时间");
        JTextField baobao3 = new JTextField(16);
        p3.add(lblPassword3);
        p3.add(baobao3);
        frmMain.add(p3);

        JPanel p4 = new JPanel();
        JLabel lblPassword4 = new JLabel("地点");
        JTextField baobao4 = new JTextField(16);
        p4.add(lblPassword4);
        p4.add(baobao4);
        frmMain.add(p4);

        JPanel p5 = new JPanel();
        JLabel lblPassword5 = new JLabel("颜色");
        JTextField baobao5 = new JTextField(16);
        p5.add(lblPassword5);
        p5.add(baobao5);
        frmMain.add(p5);
        JPanel p6 = new JPanel();
        JLabel lblPassword6 = new JLabel("头图大小");
        JTextField baobao6 = new JTextField(16);
        p6.add(lblPassword6);
        p6.add(baobao6);
        frmMain.add(p6);
        JPanel p7 = new JPanel();
        JLabel lblPassword7 = new JLabel("宝宝(宝宝微信图片)");
        JTextField baobao7 = new JTextField(16);
        p7.add(lblPassword7);
        p7.add(baobao7);
        frmMain.add(p7);
        JPanel p8 = new JPanel();
        JLabel lblPassword8 = new JLabel("手机号码(宝宝微信)");
        JTextField baobao8 = new JTextField(16);
        p8.add(lblPassword8);
        p8.add(baobao8);
        frmMain.add(p8);

        JPanel p9 = new JPanel();
        JLabel lblPassword9 = new JLabel("pid");
        JTextField baobao9 = new JTextField(16);
        p9.add(lblPassword9);
        p9.add(baobao9);
        frmMain.add(p9);
        JPanel p10 = new JPanel();
        JLabel lblPassword10 = new JLabel("pidFor(循环报名pid)");
        JTextField baobao10 = new JTextField(16);
        p10.add(lblPassword10);
        p10.add(baobao10);
        frmMain.add(p10);
        JPanel p11 = new JPanel();
        JLabel lblPassword11 = new JLabel("轻松装老余头温馨提示");
        JTextField baobao11 = new JTextField(16);
        p11.add(lblPassword11);
        p11.add(baobao11);
        frmMain.add(p11);

        JPanel p12 = new JPanel();
        JLabel lblPassword12 = new JLabel("选择pc端图片");
        JButton open=new JButton("打开文件选择器");
        p12.add(lblPassword12);
        p12.add(open);
        open.addActionListener(new FileActionListener(lblPassword12));
        frmMain.add(p12);


        JPanel p13= new JPanel();
        JLabel lblPassword13 = new JLabel("选择sj端图片");
        JButton open2=new JButton("打开文件选择器");
        p13.add(lblPassword13);
        p13.add(open2);
        open2.addActionListener(new FileActionListener(lblPassword13));
        frmMain.add(p13);

        // JPanel p14= new JPanel();
        // JLabel lblPassword14 = new JLabel("模板路径");
        // JButton open3=new JButton("打开文件选择器");
        // p14.add(lblPassword14);
        // p14.add(open3);
        // open3.addActionListener(new FileActionListener(lblPassword14));
        // frmMain.add(p14);



        // 按钮组件
        JButton btnOK = new JButton("生成");
        // 同时设置组件的大小和位置
        btnOK.setBounds(50, 50, 100, 30);

        btnOK.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {

                    ActivityProperties activityProperties=new ActivityProperties();
                    activityProperties.setTheme(baobao2.getText());
                    activityProperties.setActivityTime(baobao3.getText());
                    activityProperties.setActivityAddr(baobao4.getText());
                    activityProperties.setActivityHint(baobao11.getText());
                    activityProperties.setActivityUrl(baobao1.getText());
                    activityProperties.setActivityName(baobao1.getText());
                    activityProperties.setBboWx(baobao8.getText());
                    activityProperties.setJiaBB(baobao7.getText());
                    activityProperties.setPid(baobao9.getText());
                    activityProperties.setPidFor(baobao10.getText());
                    activityProperties.setColor(baobao5.getText());
                    activityProperties.setPx(baobao6.getText());

                    String path1 = lblPassword12.getText();
                    String path2 = lblPassword13.getText();
                    String property = System.getProperty("user.dir")+"/template";
                    System.out.println(System.getProperty("user.dir"));
                    production(activityProperties,path1,path2,property);
                } catch (IOException ex) {
                    ex.printStackTrace();
                } catch (TemplateException ex) {
                    ex.printStackTrace();
                }
            }
        });
        // 把按钮加入到主窗体中
        frmMain.add(btnOK);
        // 关闭窗体的时候，退出程序
        frmMain.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // 让窗体变得可见
        frmMain.setVisible(true);

    }

    private static String createJpanel(JFrame frmMain, String s) {
        JPanel p6 = new JPanel();
        JLabel lblPassword5 = new JLabel(s);
        JTextField baobao = new JTextField(16);
        p6.add(lblPassword5);
        p6.add(baobao);
        frmMain.add(p6);
        return baobao.getText();
    }


}
